package com.dolceplan.dolceapp;

import android.location.Location;
import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {
	private TextView offerNumber;
	private TextView latitude;
	private TextView longitude;
	private static final String TAG = "DolceAppService";
	private Intent intent;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        offerNumber = (TextView)findViewById(R.id.textView1);
        latitude = (TextView)findViewById(R.id.textView2);
        longitude = (TextView)findViewById(R.id.textView3);
        intent = new Intent(this, DolceAppService.class);               
    }

    private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
        	updateUI(intent);       
        }
               
    };
    
    @Override
	public void onResume() {
		super.onResume();		
		startService(intent);
		registerReceiver(broadcastReceiver, new IntentFilter(DolceAppService.BROADCAST_ACTION));
	}

	@Override
	public void onPause() {
		super.onPause();
		unregisterReceiver(broadcastReceiver);
		stopService(intent); 		
	}
    
    private void updateUI(Intent intent) {
    	DolceApp dolceApp = (DolceApp) getApplication();
    	String number = String.valueOf(dolceApp.getOfferList().size());
    	String lat = String.valueOf(dolceApp.getLocation().getLatitude());
    	String lon = String.valueOf(dolceApp.getLocation().getLongitude());
    	offerNumber.setText(number);
    	latitude.setText(lat);
    	longitude.setText(lon);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}
